﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace LEEAAttendanceControl
{
    public partial class App : Application
    {
        public event EventHandler ChangeEmployeesEvent;
        public event EventHandler ChangeReportTitleEvent;
        public event EventHandler RefreshAttendanceStatistics;

        public event EventHandler ReloadSpecificSummaryEvent;
        
        public event EventHandler AddEvent;
        public event EventHandler EditEvent;
        public event EventHandler DeleteEvent;

        public event EventHandler AddCompanyEvent;
        public event EventHandler EditCompanyEvent;
        public event EventHandler DeleteCompanyEvent;

        public event EventHandler AddDepartmentEvent;
        public event EventHandler EditDepartmentEvent;
        public event EventHandler DeleteDepartmentEvent;

        public event EventHandler ReloadTree;


        private bool _IsBusy=false;

        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
            }
        }

        private string reportName;

        public string ReportName
        {
            get { return reportName; }
            set
            {
                reportName = value;

                if (ChangeReportTitleEvent != null)
                {
                    ChangeReportTitleEvent(null, new EventArgs());
                }
            }
        }

      
        private int? companyId = null;

        public int? CompanyID
        {
            get { return companyId; }
            set { companyId = value; }
        }

        private int? departmentId = null;

        public int? DepartmentID
        {
            get { return departmentId; }
            set { departmentId = value; }
        }

        private DateTime perionFrom;

        public DateTime PeriodFrom
        {
            get { return perionFrom; }
            set { perionFrom = value; }
        }

        private DateTime periodTo;

        public DateTime PeriodTo
        {
            get { return periodTo; }
            set
            {
                periodTo = value;
                
                if (ChangeEmployeesEvent != null)
                {
                    ChangeEmployeesEvent(null, new EventArgs());
                }

                if (ReloadSpecificSummaryEvent != null)
                {
                    ReloadSpecificSummaryEvent(null, new EventArgs());
                }
            }
        }

        public int EmployeeId { get; set; }

        private string userName;

        public String UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        public string DisplayName { get; set; }

        private string userRole;

        public string UserRole
        {
            get { return userRole; }
            set { userRole = value; }
        }

        public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            this.RootVisual = new MainPage();
        }

        private void Application_Exit(object sender, EventArgs e)
        {

        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            // If the app is running outside of the debugger then report the exception using
            // the browser's exception mechanism. On IE this will display it a yellow alert 
            // icon in the status bar and Firefox will display a script error.
            if (!System.Diagnostics.Debugger.IsAttached)
            {

                // NOTE: This will allow the application to continue running after an exception has been thrown
                // but not handled. 
                // For production applications this error handling should be replaced with something that will 
                // report the error to the website and stop the application.
                e.Handled = true;
                Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
            }
        }

        private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
        {
            try
            {
                string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
                errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

                System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
            }
            catch (Exception)
            {
            }
        }

        public void ClearEvents()
        {

            if (ChangeEmployeesEvent != null)
            {
                foreach (EventHandler handler in ChangeEmployeesEvent.GetInvocationList())
                {
                    ChangeEmployeesEvent -= handler;
                }
            }


            if (AddEvent != null)
            {
                foreach (EventHandler handler in AddEvent.GetInvocationList())
                {
                    AddEvent -= handler;
                }
            }

            if (EditEvent != null)
            {
                foreach (EventHandler handler in EditEvent.GetInvocationList())
                {
                    EditEvent -= handler;
                }
            }

            if (DeleteEvent != null)
            {
                foreach (EventHandler handler in DeleteEvent.GetInvocationList())
                {
                    DeleteEvent -= handler;
                }
            }
        }

        //--------------------------------------------------------------------

        public void InvokeChangeEmployeesEvent(object sender, EventArgs e)
        {
            if (ChangeEmployeesEvent != null)
            {
                ChangeEmployeesEvent(sender, e);
            }
        }

        //--------------------------------------------------------------------

        public void InvokeAddEvent(object sender, EventArgs e)
        {
            if (AddEvent != null)
            {
                AddEvent(sender, e);
            }
        }

        public void InvokeEditEvent(object sender, EventArgs e)
        {
            if (EditEvent != null)
            {
                EditEvent(sender, e);
            }
        }

        public void InvokeDeleteEvent(object sender, EventArgs e)
        {
            if (DeleteEvent != null)
            {
                DeleteEvent(sender, e);
            }
        }

        //--------------------------------------------------------------------

        public void InvokeAddCompanyEvent(object sender, EventArgs e)
        {
            if (AddCompanyEvent != null)
            {
                AddCompanyEvent(sender, e);
            }
        }

        public void InvokeEditCompanyEvent(object sender, EventArgs e)
        {
            if (EditCompanyEvent != null)
            {
                EditCompanyEvent(sender, e);
            }
        }

        public void InvokeDeleteCompanyEvent(object sender, EventArgs e)
        {
            if (DeleteCompanyEvent != null)
            {
                DeleteCompanyEvent(sender, e);
            }
        }

        //--------------------------------------------------------------------

        public void InvokeAddDepartmentEvent(object sender, EventArgs e)
        {
            if (AddDepartmentEvent != null)
            {
                AddDepartmentEvent(sender, e);
            }
        }

        public void InvokeEditDepartmentEvent(object sender, EventArgs e)
        {
            if (EditDepartmentEvent != null)
            {
                EditDepartmentEvent(sender, e);
            }
        }

        public void InvokeDeleteDepartmentEvent(object sender, EventArgs e)
        {
            if (DeleteDepartmentEvent != null)
            {
                DeleteDepartmentEvent(sender, e);
            }
        }

        //--------------------------------------------------------------------

        public void RefreshStructureTree(object sender, EventArgs e)
        {
            if (ReloadTree != null)
            {
                ReloadTree(sender, e);
            }
        }

        //--------------------------------------------------------------------
        public void RefreshStatistics(object sender, EventArgs e)
        {
            if (RefreshAttendanceStatistics != null)
            {
                RefreshAttendanceStatistics(sender, e);
            }
        }
    }
}
